
const locUrl = 'http://10.1.237.10/house_api/public/index.php/api/v1/';

class HTTP {
    
    request({url,method='GET',data={},requireAuth=false}){
         return new Promise((resolve,reject)=>{
            this._request(url,resolve,reject,method,data,requireAuth);
         });
    }
    
    _request(url,resolve,reject,method,data,requireAuth){

       let header = { "content-type": "application/json" };

       if(requireAuth){
         
         let token = wx.getStorageSync('token');
         if(!token){

            
            wx.showToast({
               title:'请先登录！',
               icon:'none'
            })
            
            wx.switchTab({
              url: '/pages/about/about'
            })
            
            return false;

         }else{
            header.Authorization = 'Bearer ' + token; 
         }
         
       }
       
       wx.request({
          url:locUrl+url,
          method,
          data,
          header,
          success(res){
            resolve(res.data);
          },
          fail(){
            reject();
          }
       })
        
    }
    
}

export default new HTTP();


